package com.kmxd.ams.app.event.es.command;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.kmxd.ams.client.oss.vo.EfileRecordListVO;
import com.kmxd.ams.core.common.enums.OcrStatusEnum;
import com.kmxd.ams.core.oss.template.OssTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ocr内容处理
 *
 * @author nickbi
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EsOcrCmd {

  private final OssTemplate ossTemplate;
  @Value("${easy-es.global-config.maxWordsSize:3000}")
  private Integer maxWordsSize;

  public String getOcrContentInfo(EfileRecordListVO file) {
    try {
      if (!NumberUtil.equals(OcrStatusEnum.SUCCESS.getCode(), file.getOcrStatus())) {
        return null;
      }
      String oldSaveFilePath = file.getOldSaveFilePath();
      String filePath = ossTemplate.getOcrPath(file.getDefPath()) + oldSaveFilePath;
      String content = FileUtil.readUtf8String(filePath);
      return content;
      // 如果需要限制长度,打开下面这行代码
      //      return StrUtil.sub(content, 0, maxWordsSize);
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return null;
    }
  }
}
